package com.smartHire.controller.User.Order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smartHire.dao.Order_goodsDao;
import com.smartHire.domain.*;
import com.smartHire.service.*;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.Array;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet(name = "PlaceOrderServlet", value = "/PlaceOrderServlet")
public class PlaceOrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String open_id = request.getParameter("open_id");
        String address_Id = request.getParameter("address_id");
        String goods_item = request.getParameter("goods_item");
        String good_ids = request.getParameter("good_ids");






        System.out.println(open_id);
        System.out.println(goods_item);
        System.out.println(address_Id);


        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========"+ctx);

        try {
            OrderService orderService = (OrderService) ctx.getBean("orderService");
            UserService userService = (UserService) ctx.getBean("userService");
            GoodsService goodsService = (GoodsService) ctx.getBean("goodsService");
            ShopService shopService = (ShopService) ctx.getBean("shopService");
            Order_goodsService order_goodsService = (Order_goodsService) ctx.getBean("order_goodsService");
            CartService cartService = (CartService) ctx.getBean("cartService");


            JSONArray array = JSONObject.parseArray(goods_item);


            for(Object ob: array)
            {
                Order order = new Order();


                int rows = orderService.queryOrderRows();
                order.setOrder_Id("wx" + String.format("%08d", rows+1));

                order.setU_Id(open_id);

                JSONArray array2 = JSONObject.parseArray(ob.toString());
                order.setShop_name(array2.get(1).toString());
                order.setAddress_Id(Integer.parseInt(address_Id));
                order.setGood_id(Integer.parseInt(array2.get(0).toString()));

                Date date = new Date();
                order.setOrder_time(new Timestamp(date.getTime()));
                order.setDay(Integer.parseInt(array2.get(3).toString()));
                order.setNumber(Integer.parseInt(array2.get(2).toString()));
                order.setStatus(0);
                order.setTotal_amount(Float.parseFloat(array2.get(4).toString()));

                Address address = new Address();
                address.setAddress_Id(order.getAddress_Id());
                address = userService.queryAddressByAddressId(address);
                order.setAddress(address.getAddress());

                Goods goods = new Goods();
                goods.setGood_id(order.getGood_id());
                goods = goodsService.queryGoodsByID(goods);
                goods.setSalesAmount(goods.getSalesAmount()+1);


                Shop shop = new Shop();
                shop.setShop_id(goods.getShop_id());
                shop = shopService.queryShopById(shop);

                order.setB_address(shop.getShop_address());
                order.setGood_name(goods.getGood_name());


                Order_goods order_goods = new Order_goods();
                order_goods.setOrder_Id(order.getOrder_Id());
                order_goods.setGood_id(order.getGood_id());
                order_goods.setRest(order.getDay());
                order_goods.setCost(order.getTotal_amount());

                User user = new User();
                user.setU_Id(open_id);
                user = userService.queryUserById(user);
                user.setBalance(user.getBalance()-order.getTotal_amount());


                if(orderService.insertOrder(order)==1)
                {

                    if(order_goodsService.insertOrder_goods(order_goods)==1)
                    {
                        order_goods = order_goodsService.queryOrder_goodsByOrder_Id(order_goods);
                        order.setOG_Id(order_goods.getOG_Id());

                        orderService.updateOrderOG_Id(order);

                        goods.setInventory(goods.getInventory()-1);
                        goodsService.updateGoodsInventory(goods);

                        goodsService.updateGoodsAmount(goods);
                        userService.updateBalance(user);
                        response.getWriter().write("OK");
                    }else
                    {
                        response.getWriter().write("NO");
                    }
                }else
                {
                    response.getWriter().write("NO");
                }

            }

            System.out.println("asaaaaaaaaa");
            System.out.println(good_ids);
            if(good_ids!="")
            {
                JSONArray array2 = JSONObject.parseArray(good_ids);
                for (Object a: array2)
                {
                    Map map = (Map) a;

                    Cart cart = new Cart();
                    cart.setC_id(Integer.parseInt(map.get("Goods_id").toString()));
                    cartService.deleteCart(cart);



                }
            }



        }catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
